import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home.vue'
import positionDetail from '../views/PositionDetail.vue'
import IndexPage from '../views/'
import UserInfo from '../views/UserInfo.vue'
import NounRoter from './noun'
import VideoLobby from '../views/VideoLobby.vue'
import ReceivedOrAcceptedResume from '../views/ReceivedOrAcceptedResume.vue'
import SearchList from '../views/SearchList.vue'
import sock from '@/common/socket'

Vue.use(Router)

const router = new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'index',
      component: IndexPage,
      children: [
        {
          path: '/',
          name: 'home',
          component: Home,
          meta: { requiresAuth: true }
        },
        {
          path: '/position/:id',
          name: 'positionDetail',
          component:positionDetail,
          // meta: { requiresAuth: true }
        },
        {
          path: '/userinfo',
          name: 'userinfo',
          component:UserInfo,
        },
        {
          path: '/videolobby/:jobFairId/:state',
          name: 'videolobby',
          component:VideoLobby,
        },
        {
          path: '/search/:searchSelectValue/:searchValue',
          name: 'search',
          component:SearchList,
        },
        {
          path: '/receivedoracceptedresume',
          name: 'receivedoracceptedresume',
          component:ReceivedOrAcceptedResume,
        },
        ...NounRoter
      ]
    },
    {
      path: '/login/',
      meta: { requiresAuth: true },
      redirect: to => {
        return '/login/0'
      }
    },
    {
      path: '/login/:userType',
      name: 'login',
      meta: { requiresAuth: true },
      component: () => import('../views/Login.vue')
    },
    {
      path: '/register',
      meta: { requiresAuth: true },
      redirect: to => {
        return '/register/0'
      }
    },
    {
      path: '/register/:userType',
      name: 'register',
      meta: { requiresAuth: true },
      component: () => import('../views/Register.vue')
    },
    {
      path: '/forgetpass',
      name: 'forgetpass',
      meta: { requiresAuth: true },
      component: () => import('../views/ForgetPass.vue')
    }
  ]
})

// const loading = router.app.$loading({
//   lock: true,
//   text: 'Loading',
//   spinner: 'el-icon-loading',
//   background: 'rgba(255, 255, 255, 0.7)'
// })

router.beforeEach((to, from, next) => {
  !localStorage.getItem("user_data") && localStorage.setItem("user_data",'')
  router.app.$options.store.dispatch('loginLocalstorToState');
  // console.log(to.meta.requiresAuth,router.app.$options.store.state.home.isLogin)
  if(to.meta.requiresAuth == undefined && !router.app.$options.store.state.home.isLogin){
    next({path: '/'})
    router.app.$message({
      dangerouslyUseHTMLString: true,
      message: '<div>未登录，请<a href="/#/login">点击登陆</a></div>'
    })
    return
  }

  // if(to.name == 'videolobby'){
  //   router.app.$loading({
  //     lock: true,
  //     text: 'Loading',
  //     spinner: 'el-icon-loading',
  //     background: 'rgba(255, 255, 255, 0.7)'
  //   })
  // }
  
  next()
})

router.afterEach((to, from) =>{
  if(from.name == 'videolobby'){
    if(router.app.$options.store.state.home.isLogin==true){
      sock.emit('leaveJobFair', {
        jobFairId: from.params.jobFairId,
        userId: router.app.$options.store.state.home.globalUserInfo.userId
      })
    }
    
  }
  
})

export default router;